<?php
namespace Home\Controller;

use My\Model\DistributionShopOrderModel;
use Utuo\DistributionShopOrderController;

class DisShopCoquetryOrderController extends DistributionShopOrderController {
	
	/**
	 * 初始化
	 */
	public function _init() {
		layout ( 'disshop' );
		$this->setModelName ( "DistributionShop" );
		$this->setModelControllerName( "DisShop" );
		$this->cart = $this->getCart ();
		if ( in_array(ACTION_NAME, array('anotherConfirmOrder', 'sharePayAnother')) ) {
			$this->setSupportRedirect( true );
		}
		if ( ACTION_NAME == "signIn" ) {
			\Think\Hook::add('disorder_completed', 'Home\\Behaviors\\DistributionShopOrderCompletedBehavior');
		}
	}
	
	/**
	 * 发起代付页
	 */
	public function payAnotherAction() {
		$orderId = I('id', 0, "intval");
		$order = $this->getOrder ( $orderId );
		$this->assign ( "order", $order );
		$this->display("{$this->modelControllerName}/payAnother");
	}
	
	/**
	 * 发起代付
	 */
	public function payAnotherPostAction() {
		$id = I("id", 0, "intval");
		$say = I("say");
		$order = $this->getOrder ( $id );
		if (!$order or $order['uid'] != $this->uid ) {
			$this->error("订单没有找到");
		}
		$m = M("DistributionShopCoquetry");
		$coquetry = $m->where("order_id=%d", $id)->find();
		if ( !$coquetry ) {
			$m->add(array(
				'order_id' => $order['id'],
				'total' => $order['actual_cost'],
				'sell' => 0,
				'progress' => 0,
				'say' => $say,
				'create_time' => time(),
				'buy_status' => DistributionShopOrderModel::ORDER_COQUETRY_STATUS_SAVED,
			));
			if ( $order['buy_method'] != DistributionShopOrderModel::BUY_METHOD_COQUETRY ) {
				M("DistributionShopOrder")->where(array(
					'id' => $order['id'],
				))->setField('buy_method', DistributionShopOrderModel::BUY_METHOD_COQUETRY );
			}
		} else {
			$m->where("order_id=%d", $id)->save(array( 'say' => $say, ));
		}
		redirect(U("DisShopCoquetryOrder/sharePayAnother", "sid={$this->sid}&id={$id}"));
	}
	
	/**
	 * 分享预览页
	 */
	public function sharePayAnotherAction() {
		$id = I("id", 0, "intval");
		$order = $this->getOrder ( $id );
		$orderItemFirst = current($order['orderItems']);
		$this->setShare(array(
				'link' => U('DisShopCoquetryOrder/anotherConfirmOrder', array('id' => $id, 'sid' => $this->cid ), false, true),
				'img' => $orderItemFirst['items'][0]['productPic'],
				'title' => $order['say'],
				'desc' => $order['say'],
		));
		$this->assign("order", $order);
		$this->display("{$this->modelControllerName}/sharePayAnother");
	}
	
	/**
	 * 代付人支付页(发起人分享出去的页面)
	 */
	public function anotherConfirmOrderAction() {
		
	}
	
	/**
	 * 代付人支付提交
	 */
	public function anotherConfirmOrderPostAction() {
		
	}
	
	/**
	 * 代付信息页
	 */
	public function payAnotherInfoAction() {
		$id = I("id", 0, "intval");
		$order = $this->getPayingOrder( $id );
		$orderItemFirst = current($order['orderItems']);
		$this->setShare(array(
				'link' => U('DisShopCoquetryOrder/anotherConfirmOrder', array('id' => $id, 'sid' => $this->cid ), false, true),
				'img' => $orderItemFirst['items'][0]['productPic'],
				'title' => $order['say'],
				'desc' => $order['say'],
		));
		$this->assign("order", $order);
		$this->display("{$this->modelControllerName}/payAnotherInfo");
	}
	
	/**
	 * 订单结算页
	 */
	public function confirmOrderAction() {
		$orderId = I('orderId', 0, "intval");
		$order = array();
		if ( $orderId ) {
			$order = $this->getPayingOrder( $orderId );
		} else {
			$order = $this->getSaveingOrder();
			session ( "order", $order );
		}
		$this->assign ( "orderJSON", json_encode ( $order ) );
		$this->assign ( 'order', $order );
		$this->assign ( "buyUrl", urlencode($this->getCurrentUrl ()) );
		$this->display ( "{$this->modelControllerName}/" . ACTION_NAME."Coquetry" );
	}
	
	/**
	 * 获得一个订单
	 * @param unknown $id
	 * @return array
	 */
	protected function getOrder($id) {
		return D( "My/DistributionShopOrder" )->getOrder($id);
	}
	
}